package com.bawei.module_publish.openlive.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.lib_mvi.base.BaseViewModel
import com.bawei.module_publish.openlive.intent.LiveIntent
import com.bawei.module_publish.openlive.model.repository.LiveRepository
import com.bawei.module_publish.openlive.state.LiveState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch

class LiveViewModel: BaseViewModel<LiveIntent>() {
    private val repository: LiveRepository by  lazy { LiveRepository() }
    private var liveOpenFlow = MutableStateFlow<LiveState>(LiveState.Init)
    val _liveOpenFlow = liveOpenFlow.asStateFlow()
    private var sendPacketFlow = MutableStateFlow<LiveState>(LiveState.Init)
    val _sendPacketFlow = sendPacketFlow.asStateFlow()
    private var onlineNumFlow = MutableStateFlow<LiveState>(LiveState.Init)
    val _onlineNumFlow = onlineNumFlow.asStateFlow()
    private var deleteLiveNumFlow = MutableStateFlow<LiveState>(LiveState.Init)
    val _deleteLiveNumFlow = deleteLiveNumFlow.asStateFlow()
    private var myGoodsNumFlow = MutableStateFlow<LiveState>(LiveState.Init)
    val _myGoodsNumFlow = myGoodsNumFlow.asStateFlow()
    private var topCarFlow = MutableStateFlow<LiveState>(LiveState.Init)
    val _topCarFlow = topCarFlow.asStateFlow()
    private var getGoodsFlow = MutableStateFlow<LiveState>(LiveState.Init)
    val _getGoodsFlow = getGoodsFlow.asStateFlow()
    private var explainGoodsFlow = MutableStateFlow<LiveState>(LiveState.Init)
    val _explainGoodsFlow = explainGoodsFlow.asStateFlow()
    private var getSquareGoodsFlow = MutableStateFlow<LiveState>(LiveState.Init)
    val _getSquareGoodsFlow = getSquareGoodsFlow.asStateFlow()
    private var addSquareGoodsFlow = MutableStateFlow<LiveState>(LiveState.Init)
    val _addSquareGoodsFlow = addSquareGoodsFlow.asStateFlow()
    private var addAdFlow = MutableStateFlow<LiveState>(LiveState.Init)
    val _addAdFlow = addAdFlow.asStateFlow()
    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect{
                when(it){
                    is LiveIntent.OpenLive -> liveOpenData(it)
                    is LiveIntent.GetOnlineNum -> getOnlineNumData(it)
                    is LiveIntent.SendRedPacket -> sendPacketData(it)
                    is LiveIntent.DeleteLiveRoom -> deleteLiveNumData(it)
                    is LiveIntent.MyShowcase -> myShowcaseData()
                    is LiveIntent.TopCar -> topCarData(it)
                    is LiveIntent.LiveRoomGoods -> liveRoomGoodsData(it)
                    is LiveIntent.ExplainGoods -> explainGoodsData(it)
                    is LiveIntent.ChooseProductSquare -> getSquareGoodsData(it)
                    is LiveIntent.AddOrCancelShowcase -> addSquareGoodsData(it)
                    is LiveIntent.AdInfo -> addAdData(it)
                }
            }
        }
    }
    private fun liveOpenData(it: LiveIntent.OpenLive) {
        viewModelScope.launch {
            repository.openLive(it.body)
                .catch { e->
                    liveOpenFlow.value = LiveState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        liveOpenFlow.value = LiveState.OpenLiveSuccess(it.data)
                    }else{
                        liveOpenFlow.value = LiveState.Failed(it.msg)
                    }
                }

        }
    }
    private fun sendPacketData(it: LiveIntent.SendRedPacket) {
        viewModelScope.launch {
            repository.sendRedPacket(it.body)
                .catch { e->
                    sendPacketFlow.value = LiveState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        sendPacketFlow.value = LiveState.SendPacketSuccess(it.data)
                    }else{
                        sendPacketFlow.value = LiveState.Failed(it.msg)
                    }
                }
        }
    }
    private fun getOnlineNumData(it: LiveIntent.GetOnlineNum) {
        viewModelScope.launch {
            repository.getOnlineNum(it.roomId)
                .catch { e->
                    onlineNumFlow.value = LiveState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        onlineNumFlow.value = LiveState.GetOnlineNumSuccess(it.data)
                    }else{
                        onlineNumFlow.value = LiveState.Failed(it.msg)
                    }
                }
        }
    }
    private fun deleteLiveNumData(it: LiveIntent.DeleteLiveRoom) {
        viewModelScope.launch {
            repository.deleteLiveRoom(it.roomId)
                .catch { e->
                    deleteLiveNumFlow.value = LiveState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        deleteLiveNumFlow.value = LiveState.DeleteLiveRoomSuccess(it.data)
                    }else{
                        deleteLiveNumFlow.value = LiveState.Failed(it.msg)
                    }
                }
        }
    }
    private fun myShowcaseData() {
        viewModelScope.launch {
            repository.myShowcase()
                .catch { e->
                    myGoodsNumFlow.value = LiveState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        myGoodsNumFlow.value = LiveState.GetGoodsSuccess(it.data)
                    }else{
                        myGoodsNumFlow.value = LiveState.Failed(it.msg)
                    }
                }
        }
    }
    private fun topCarData(it: LiveIntent.TopCar) {
        viewModelScope.launch {
            repository.topCar(it.body)
                .catch { e->
                    topCarFlow.value = LiveState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        topCarFlow.value = LiveState.TopCarSuccess(it.data)
                    }else{
                        topCarFlow.value = LiveState.Failed(it.msg)
                    }
                }
        }
    }
    private fun liveRoomGoodsData(it: LiveIntent.LiveRoomGoods) {
        viewModelScope.launch {
            repository.liveRoomGoods(it.roomId)
                .catch { e->
                    getGoodsFlow.value = LiveState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        getGoodsFlow.value = LiveState.GetGoodsListSuccess(it.data)
                    }else{
                        getGoodsFlow.value = LiveState.Failed(it.msg)
                    }
                }
        }
    }
    private fun explainGoodsData(it: LiveIntent.ExplainGoods) {
        viewModelScope.launch {
            repository.explainGoods(it.body)
                .catch { e->
                    explainGoodsFlow.value = LiveState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        explainGoodsFlow.value = LiveState.ExplainGoodsSuccess(it.data)
                    }else{
                        explainGoodsFlow.value = LiveState.Failed(it.msg)
                    }
                }
        }
    }
    private fun getSquareGoodsData(it: LiveIntent.ChooseProductSquare) {
        viewModelScope.launch {
            repository.chooseProductSquare(it.body)
                .catch { e->
                    getSquareGoodsFlow.value = LiveState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        getSquareGoodsFlow.value = LiveState.GetSquareGoodsSuccess(it.data)
                    }else{
                        getSquareGoodsFlow.value = LiveState.Failed(it.msg)
                    }
                }
        }
    }
    private fun addSquareGoodsData(it: LiveIntent.AddOrCancelShowcase) {
        viewModelScope.launch {
            repository.addOrCancelShowcase(it.goodsId)
                .catch { e->
                    addSquareGoodsFlow.value = LiveState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        addSquareGoodsFlow.value = LiveState.AddOrCancelGoodsSuccess(it.data)
                    }else{
                        addSquareGoodsFlow.value = LiveState.Failed(it.msg)
                    }
                }
        }
    }
    private fun addAdData(it: LiveIntent.AdInfo) {
        viewModelScope.launch {
            repository.adInfo(it.body)
                .catch { e->
                    addAdFlow.value = LiveState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        addAdFlow.value = LiveState.AdInfoSuccess(it.data)
                    }else{
                        addAdFlow.value = LiveState.Failed(it.msg)
                    }
                }
        }
    }
}